<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller {
	
	public function index()
	{
		$this->lang->load('site', get_lang());
		$data['page'] = 'pages/whoweare';
		$this->load->view('mainview', $data);
	}
	
	public function profile()
	{
		redirect(base_url().'profile');
	}
	
		
	public function send(){
		$config = $this->input->post();
		
		//echo $this->lang->line('msg_is_alredy_registred');
		
		$this->load->model('Users');
		$this->load->helper(array('form', 'url', 'language'));
		$this->load->library(array('form_validation', 'user_agent'));
		
		$this->form_validation->set_rules('type', 'Type of user', 'required');
		$this->form_validation->set_rules('name', 'Name', 'required');
		$this->form_validation->set_rules('email', 'E-mail', 'is_unique[users.nm_email]');
		$this->form_validation->set_rules('pass', 'Password', 'required');
		$this->form_validation->set_message('required', 'The field %s is required!');
		
		if($this->agent->accept_lang('pt')){
			$this->lang->load("site","pt");
		}else{
			$this->lang->load("site","en");
		}

		if ($this->form_validation->run() == TRUE)
		{
			$this->Users->save($config);
			$this->sendMail($this->input->post('name'), $this->input->post('email'));
			$data['page'] = 'pages/regsuccess';
		}else{
			$data['page'] = 'pages/error';
		}		
		$this->load->view('mainview', $data);
	}
	
	public function sendMail($name, $email){
		
		$this->load->library('email');
		
		$config['protocol'] = 'sendmail';
		$config['mailpath'] = '/usr/sbin/sendmail';
		$config['mailtype'] = 'html';
		$config['charset'] = 'utf-8';
		$config['useragent'] = 'wedesignclothing';
		$config['wordwrap'] = TRUE;
		
		$message = $this->load->view('email/regconfirmation', array('name'=>$name), TRUE);
		
		$this->email->initialize($config);
		$this->email->from('talkwith@wdcloth.com', 'We Design Clothing Team');
		$this->email->to($email);  
		$this->email->bcc('luiz@tecsites.com.br'); 
		$this->email->subject('You have successfully registered');
		$this->email->message($message);	
		$this->email->send();
		
		//echo $this->email->print_debugger();
	}	 
	
}
